#include "TRocker.h"
#include "../utils/Utils.h"

TRocker::TRocker(){
    m_pRockerBg=NULL;
    m_pPosNormal=NULL;
    m_pPosDown=NULL;
     m_nRadius=0;
     
}

 
void TRocker::registerWithTouchDispatcher()
{
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority, true);
}

TRocker* TRocker::create(xml_node<> * pItem){
    TRocker*  ret=TRocker::create();
    ret->initWidthConf(pItem);
    return ret;
}


void TRocker::initWidthConf(xml_node<> * pItem){
    if(!pItem) return; 
    m_nRadius = readAttrInt(pItem,"radius");
    
    
    
    readAttrString(pItem,"normalImage",normalImagePath);
    readAttrString(pItem,"downImage",downImagePath);
    
    string bgPath;
    readAttrString(pItem,"source",bgPath);
    
    m_pRockerBg=CCSprite::createWithSpriteFrameName(bgPath.c_str()); 
    addChild(m_pRockerBg);
    
    m_pPosNormal=CCSprite::create();
    m_pPosNormal->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(normalImagePath.c_str()));
    this->addChild(m_pPosNormal);
    setContentSize(m_pRockerBg->getContentSize()); 
    
    
    
    centerPoint =ccp(getContentSize().width/2,-getContentSize().height/2);
    currentPoint = centerPoint;
    onCreateComplete();
}


void TRocker::onEnter(){
    TPanel::onEnter();
    schedule(schedule_selector(TRocker::updatePos));//添加刷新函数
}

void TRocker::onExit(){ 
    TPanel::onExit();
    unschedule(schedule_selector(TRocker::updatePos));//添加刷新函数
}
void TRocker::onCreateComplete(){
     
}

void TRocker::updatePos(float dt){
    m_pPosNormal->setPosition(ccpAdd(m_pPosNormal->getPosition(),ccpMult(ccpSub(currentPoint, m_pPosNormal->getPosition()),0.5)));
    
    m_pRockerBg->setPosition(centerPoint);
}
 
//摇杆方位
CCPoint TRocker::getDirection()
{
    return  (ccpSub(centerPoint, currentPoint));
}

//摇杆力度
float TRocker::getVelocity()
{
    return ccpDistance(centerPoint, currentPoint);
}

bool TRocker::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
     
    this->setVisible(true);
    CCPoint touchPoint =touch->getLocation();
    touchPoint = getPosInTarget(touchPoint,this);
    
    int dis=m_nRadius;
    if(m_pRockerBg){
        dis=m_pRockerBg->getContentSize().width/2;
    }
    if (ccpDistance(touchPoint, centerPoint) > dis){
        return false;
    }
    currentPoint = touchPoint;
    
    m_pPosNormal->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(downImagePath.c_str()));
    
    return true;
}
void  TRocker::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
    CCPoint touchPoint =touch->getLocation();
    touchPoint = getPosInTarget(touchPoint,this);
    currentPoint = touchPoint;
    
   
    if (ccpDistance(touchPoint, centerPoint) > m_nRadius )
    { 
        centerPoint =  ccpSub( currentPoint, ccpMult(ccpNormalize(ccpSub(touchPoint, centerPoint)), m_nRadius));
          
         
    }
}
void  TRocker::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
    centerPoint =ccp(getContentSize().width/2,-getContentSize().height/2);
    currentPoint = centerPoint;
    m_pPosNormal->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(normalImagePath.c_str()));
}
 